<template>
	<om-formsub-outer :index="index" :checkedit="checkedit" :deleteBtn="false">
		<om-autoform ref="autoform" :checkedit="checkedit" :initConfigData="autoformdata" :initValue="initValue"></om-autoform>
	</om-formsub-outer>
</template>

<script>
import { subOnLoad, autoChangeEditor, changeEditor, configSelectData } from '../../../static/js/formUtil.js';
import { ignoreHtml, ptFloat, minusF, getDateStr } from '@/static/js/common.js';
export default {
	provide() {
		return {
			formPage: this
		};
	},
	data() {
		return {
			initValue: {},
			autoformdata: [],
			cachename: '',
			index: null,
			datas: [],
			order: 0,
			formdata: {},
			checkedit: false,
			frequencyObj: {},
			frequencyIdList: []
		};
	},
	watch: {
		
	},
	methods: {
		initLoad() {
			let _that = this;
			let frequencyId = _that.formdata.frequencyId;
			//频次
			_that.om
				.request('postDescription/findFrequencyList', {
					businessFormId: _that.formdata.businessFormId
				})
				.then(res => {
					_that.$refs.autoform.changeEditor('title0', 'frequencyId', 'data', configSelectData(res, 'id', 'dicname'));
					_that.$refs.autoform.initSelectIndex('title0', 'frequencyId');
				});
			//时限参数
			if (frequencyId && _that.frequencyIdList.indexOf(frequencyId) < 0) {
				let dict = _that.frequencyObj;
				autoChangeEditor(
					{
						obj: _that,
						key: 'title0',
						valueField: 'num'
					},
					{
						type: 'labelbox',
						required: false,
					}
				);
				autoChangeEditor(
					{
						obj: _that,
						key: 'title0',
						valueField: 'dateParam'
					},
					{
						type: 'numberbox',
						min: dict.minNum,
						max: dict.maxNum
					}
				);
			} else {
				autoChangeEditor(
					{
						obj: _that,
						key: 'title0',
						valueField: 'num'
					},
					{
						type: 'numberbox',
					}
				);
				autoChangeEditor(
					{
						obj: _that,
						key: 'title0',
						valueField: 'dateParam'
					},
					{
						type: 'labelbox',
						required: false,
						min: '',
						max: ''
					}
				);
			}
		},
		onSelectFrequency(item) {
			let frequencyId = item.id;
			let dict = item;
			let _that = this;
			_that.frequencyObj = item;
			_that.clearFrequency(true);
			//频次为“1次/X”的，次数默认赋值1
			if (_that.frequencyIdList.indexOf(frequencyId) < 0) {
				_that.$refs.autoform.formdata.num = 1;
				_that.$refs.autoform.changeEditor('title0', 'num', {
					type: 'labelbox',
					required: false
				});
				_that.$refs.autoform.changeEditor('title0', 'dateParam', {
					type: 'numberbox',
					min: dict.minNum,
					max: dict.maxNum
				});
			} else {
				_that.$refs.autoform.formdata.num = '';
				_that.$refs.autoform.changeEditor('title0', 'num', {
					type: 'numberbox',
				});
				_that.$refs.autoform.changeEditor('title0', 'dateParam', {
					type: 'labelbox',
					required: false,
					min: '',
					max: ''
				});
			}
		},
		/**
		 * 清空频次选项
		 */
		clearFrequency(filterSelf) {
			let _that = this;
			if (!filterSelf) {
				_that.$refs.autoform.clear('title0', 'frequencyId');
			}
			let formdata = _that.$refs.autoform.formdata;
			formdata.num = '';
			formdata.dateParam = '';
			formdata.dateRemark = '';
			formdata.estimateTime = '';
		},
		/**
		 * 时限参数 失去焦点事件
		 * @param {Object} newValue
		 */
		onSelectDateParam(newValue) {
			if (newValue) {
				let _that = this;
				var dict = _that.frequencyObj;
				if (dict) {
					//验证最大最小值
					if (dict.maxNum && dict.minNum) {
						var maxNum = ptFloat(dict.maxNum);
						var minNum = ptFloat(dict.minNum);
						if (ptFloat(newValue) > maxNum) {
							_that.om.toast('时限参数不能大于' + maxNum + '！');
							_that.$refs.autoform.formdata.dateParam = '';
						} else if (ptFloat(newValue) < minNum) {
							_that.om.toast('时限参数小于' + minNum + '！');
							_that.$refs.autoform.formdata.dateParam = '';
						} else {
							//拼接完成时限
							_that.om
								.request('postDescription/getDateRemark', {
									frequencyId: _that.$refs.autoform.formdata.frequencyId,
									dateParam: newValue
								})
								.then(res => {
									if (res.succeed) {
										var value = res.value;
										if (value) {
											_that.$refs.autoform.formdata.dateRemark = value;
										}
									} else {
										_that.om.toast(res.msg);
										_that.$refs.autoform.formdata.dateParam = '';
									}
								});
						}
					} else {
						_that.$refs.autoform.formdata.dateParam = '';
					}
				} else {
					_that.om.toast('请先选择频次！');
					_that.$refs.autoform.formdata.dateParam = '';
				}
			}
		},
	},
	onLoad(e) {
		let _that = this;
		subOnLoad(_that, e, function() {
			_that.frequencyIdList = e.frequencyIdList;
			if (e.configDataCacheKey) {
				uni.getStorage({
					key: e.configDataCacheKey,
					success: function(res) {
						//if (res.data && res.data instanceof Array) {
						if(res.data && Object.prototype.toString.call(res.data)==='[object Array]'){
							_that.autoformdata = res.data;
							Object.assign(_that.initValue, _that.formdata);
							_that.autoformdata[0].order = _that.order;
							if (_that.formdata.frequencyId) {
								//获取已有频次属性
								_that.om
									.request('dictZero/getObjById', {
										id: _that.formdata.frequencyId
									})
									.then(res => {
										if (res.succeed) {
											_that.frequencyObj = res.value;
											_that.initLoad();
										} else {
											_that.om.toast(res.msg);
										}
									});
							} else {
								_that.initLoad();
							}
						}
					}
				});
			}
		});
	},
	onShow() {
		let _that = this;
		if (_that.$refs.autoform) {
		}
	}
};
</script>

<style></style>
